package com.project2.controller;

import com.project2.bean.PageBo;
import com.project2.bean.ResultVo;
import com.project2.bean.po.MarketFootprint;
import com.project2.bean.po.MarketUser;
import com.project2.bean.vo.WxGoodsDetailVo;
import com.project2.bean.vo.WxGoodsListVo;
import com.project2.mapper.MarketFootprintMapper;
import com.project2.service.WxGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author ：HsDsg
 * @date ：2022/8/14 20:40
 * @description：TODO
 */
@RestController
@RequestMapping("wx/goods")
public class WxGoodsController {

    @Autowired
    WxGoodsService goodsService;

    @Autowired
    MarketFootprintMapper marketFootprintMapper;


    /*
    TODO 商品总数
     */
    @RequestMapping("count")
    public ResultVo goodsCount() {
        long goodsCount = goodsService.goodsCount();
        return ResultVo.ok(goodsCount);
    }

    /*
    TODO 商品分类id找其他商品分类
     */
    @RequestMapping("category")
    public ResultVo getGoodsByCategoryId(Integer id) {
        ResultVo result = goodsService.getRestsCategoryByCategoryId(id);
        return result;
    }

    /*
    TODO 商品分类id找商品
     */

    // public ResultVo list(Integer categoryId, Integer page, Integer limit) {
    //     WxGoodsListVo goodsBYCategoryId = goodsService.getGoodsByCategoryId(categoryId, page, limit);
    //     return ResultVo.ok(goodsBYCategoryId);
    // }
    @RequestMapping("list")
    public ResultVo list(Integer brandId, Integer categoryId, String keyword, PageBo pageBo, HttpServletRequest request) {
        if (keyword != null) {
            String header = request.getHeader("X-CskaoyanMarket-Token");
            MarketUser user = (MarketUser) request.getSession().getAttribute(header);
            if (user == null) {
                return ResultVo.ok("请登录", 501);
            }
            WxGoodsListVo goodsByKeyword = goodsService.getGoodsByKeyword(keyword, pageBo, user);
            return ResultVo.ok(goodsByKeyword);
        }
        if (categoryId != null) {
            WxGoodsListVo goodsBYCategoryId = goodsService.getGoodsByCategoryId(categoryId, pageBo.getPage(), pageBo.getLimit());
            return ResultVo.ok(goodsBYCategoryId);
        }
        if (brandId != null) {
            WxGoodsListVo goodsByBrandId = goodsService.getGoodsByBrandId(brandId, pageBo.getPage(), pageBo.getLimit());
            return ResultVo.ok(goodsByBrandId);
        }


        return null;
    }


    /*
    TODO 商品信息
     */
    @RequestMapping("detail")
    public ResultVo detail(Integer id, HttpServletRequest request) {
        WxGoodsDetailVo goodsDetail = goodsService.getGoodsDetail(id);

        // todo: 每点击一次detail，就会生成一条历史足迹，
        //  要把生成的历史足迹加入数据库表market_footprint
        String header = request.getHeader("X-CskaoyanMarket-Token");
        MarketUser marketUser = (MarketUser) request.getSession().getAttribute(header);
        if (marketUser != null) {
            Integer userId = marketUser.getId();
            goodsService.addFootprintByGoodsIdAndUserId(id, userId);
            // todo: 根据goodsId、userId、deleted查询出浏览历史足迹，
            //  如果有多条，取add_time最大的那一条展示，其他的做逻辑删除
            //Boolean deleted = false;
            int deleted = 0;
            // todo: 查询出market_footprint表的所有goodId，并去重
            List<Integer> goodsIdList = goodsService.queryAllGoodsId();
            //List<MarketFootprint> marketFootprintList = null;
            for (Integer goodsId : goodsIdList) {
                List<MarketFootprint> marketFootprintList = goodsService.queryFootprintByGoodsIdAndUserIdAndDeleted(goodsId, userId, deleted);
                // todo: 下面这段代码放在代码外面时，marketFootprintList
                //  是局部变量，那么它会为null!!!
                if (marketFootprintList.size() == 0) {
                    //break;
                    continue;
                }

                //long maxAddTime = marketFootprintList.get(0).getAddTime().getTime();
                Date maxAddTime = marketFootprintList.get(0).getAddTime();
                for (MarketFootprint marketFootprint : marketFootprintList) {
                    //if (marketFootprint.getAddTime().compareTo(maxAddTime) > 0) {
                    if (marketFootprint.getAddTime().compareTo(maxAddTime) >= 0) {
                        maxAddTime = marketFootprint.getAddTime();
                    }
                }

                for (MarketFootprint footprint : marketFootprintList) {
                    // todo: 取add_time最大的那一条展示，其他的做逻辑删除
                    // todo: 年月日相同的数据，取add_time最大的那一条展示，其他的做逻辑删除
                    if (!footprint.getAddTime().equals(maxAddTime)) {
                        // todo: 做逻辑删除 + user genereted
                        goodsService.deleteFootprintById(footprint.getId());
                    }
                }

            }
        }


        return ResultVo.ok(goodsDetail);
    }

    /*
    TODO 大家都在看
     */
    @RequestMapping("related")
    public ResultVo related(Integer id) {
        WxGoodsListVo relatedGoods = goodsService.getRelatedGoods(id);
        return ResultVo.ok(relatedGoods);
    }
}
